昨天我們學會了 ReAct Agent,能處理多步推理。
今天,我們要做一個更實用的工具 —— 自動摘要 Agent。
它能幫你把長文章、新聞、甚至研究報告,快速濃縮成精簡重點。
應用場景:
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain_google_genai import ChatGoogleGenerativeAI
import os
# -------------------------------
# 1. API Key 設定
# -------------------------------
os.environ["GOOGLE_API_KEY"] = "你的 Gemini API Key"
# -------------------------------
# 2. 初始化 LLM
# -------------------------------
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0)
# -------------------------------
# 3. 建立摘要模板
# -------------------------------
template = """請將以下文章整理成 3 點重點:
文章內容:
{text}"""
prompt = PromptTemplate(input_variables=["text"], template=template)
# -------------------------------
# 4. 建立 Chain
# -------------------------------
summary_chain = LLMChain(llm=llm, prompt=prompt)
# -------------------------------
# 5. 測試文章
# -------------------------------
article = """
LangChain 是一個幫助開發者快速構建 LLM 應用的框架,
可以串接工具、記憶上下文,甚至建立問答型 Agent。
"""
summary = summary_chain.run(article)
print("【自動摘要】\n", summary)
📌 範例輸出為:
【自動摘要】
以下是將文章整理成的 3 點重點:
1. **LangChain 是一個幫助開發者快速構建 LLM 應用的框架。**
2. **它能串接工具、記憶上下文。**
3. **甚至能建立問答型 Agent。**